/*
 * FormatOutput.c
 *
 *  Created on: 01.10.2011
 *      Author: wink
 */

#include <avr/io.h>
#include <string.h>
#include <stdlib.h>
#include "TimeDate.h"
#include "DCF77.h"
#include "Alarm.h"
#include "lcd.h"
#include "Globals.h"
#include "DisplayControl.h"
#include "SoundControl.h"
#include "DisplayOutput.h"
#include "Menu.h"

#define CLOCK_LINE_U8   ((uint8_t) (0))
#define DATE_LINE_U8    ((uint8_t) (1))
#define ALARM_LINE_U8   ((uint8_t) (2))
#define MENU_LINE_U8    ((uint8_t) (1))

const char DsoDayAbbreviation_ch[8][4] = { {"   \0"}, {"Mon\0"}, {"Tue\0"}, {"Wed\0"}, {"Thu\0"}, {"Fri\0"}, {"Sat\0"}, {"Sun\0"}};
static void DsoClearLine( uint8_t iLine_u8 );

T_Bool DsoWriteMainView( void )
{
  DsoWriteClock();
  DsoWriteNextAlarm( );
  return (TRUE);
}

void DsoWriteClock( void )
{
    char lText_ch[DISP_WIDTH];
    T_TimeDate lTimeDate_t;
    TidGetTimeDate( &lTimeDate_t );
    lcd_gotoxy(5, CLOCK_LINE_U8);
    DspWriteTime( (T_Time*) &lTimeDate_t.Time, lText_ch, 0 );
    lcd_puts(lText_ch);
    lcd_gotoxy(4, DATE_LINE_U8);
    DsoWriteDate( (T_Date*) &lTimeDate_t.Date, lText_ch, 0 );
    lcd_puts(lText_ch);

    /* write sync symbol if dcf sync is established */
    lcd_gotoxy(DISP_WIDTH-1, CLOCK_LINE_U8 );
    if (DcfGetSyncState())
    {
      lcd_putc(0x00);
    }
    else
    {
      lcd_putc(' ');
    }
    /* write bell if any alarm is active */
    lcd_gotoxy( 0, CLOCK_LINE_U8 );
    if (0 != AlaIsAnyAlarmEnabled())
    {
      lcd_putc(0x01);
    }
    else
    {
      lcd_putc(' ');
    }
}

void DsoWriteNextAlarm( void )
{
  char lText_ch[DISP_WIDTH];
  T_TimeDate lTimeDate_t;
  T_Time lAlarmTime_t;
  TidGetTimeDate( &lTimeDate_t );
  if( ALARM_NONE_U8 != AlaGetTimeOfNextAlarm( &lAlarmTime_t ))
  {
    uint8_t lBeepMode_u8 = ScoGetBeepMode();
    lcd_gotoxy( 0, ALARM_LINE_U8 );
    lcd_putc( 0x30 + AlaGetState() );
    lcd_gotoxy( 5, ALARM_LINE_U8 );
    DspWriteTime( &lAlarmTime_t, lText_ch, 0 );
    lcd_puts(lText_ch);
    lcd_gotoxy( DISP_WIDTH-1 ,ALARM_LINE_U8);
    if( 0x10 > lBeepMode_u8 )
    {
      lcd_putc( 0x30 + lBeepMode_u8 );
    }
    else
    {
      lcd_putc( ' ');
    }
  }
  else
  {
    DsoClearLine( ALARM_LINE_U8 );
  }
}

void DspWriteTime( T_Time* iTime, char *buffer, uint8_t iFormat_u8 )
{
  if (iFormat_u8)
  {
    /* TODO (11-09-20): add format switch */
    char temp[5];
    /* Hours */
    itoa(iTime->Hour, temp, 10);
    if ( iTime->Hour < 10)
    {
      strcpy(buffer, "0");
    }
    else
    {
      strcpy(buffer, "");
    }
    strcat(buffer, temp);
    /* Minutes */
    if (iTime->Minute < 10)
    {
      strcat(buffer, ":0");
    }
    else
    {
      strcat(buffer, ":");
    }
    itoa(iTime->Minute, temp, 10);
    strcat(buffer, temp);
    /* Seconds */
    if (iTime->Second < 10)
    {
      strcat(buffer, ":0");
    }
    else
    {
      strcat(buffer, ":");
    }
    itoa(iTime->Second, temp, 10);
    strcat(buffer, temp);
  }
  else
  {
    /* TODO (11-09-20): add format switch */
    char temp[5];
    /* Hours */
    itoa(iTime->Hour, temp, 10);
    if ( iTime->Hour < 10)
    {
      strcpy(buffer, "  0");
    }
    else
    {
      strcpy(buffer, "  ");
    }
    strcat(buffer, temp);
    if( 0 == iTime->Second%2 )
    {
      strcat(buffer, ":");
    }
    else
    {
      strcat(buffer, " ");
    }
    /* Minutes */
    if (iTime->Minute < 10)
    {
      strcat(buffer, "0");
    }
    else
    {
      strcat(buffer, "");
    }
    itoa(iTime->Minute, temp, 10);
    strcat(buffer, temp);
  }
}

void DsoWriteDate( T_Date* iDate, char *buffer, uint8_t iFormat )
{
  /*
   * results in the output:
   * DD.MM.YY
   */
  /* TODO (11-09-20): add format switch */
  char temp[5];
  if( iDate->WeekDay <= Sunday )
  {
    strcpy(buffer, DsoDayAbbreviation_ch[iDate->WeekDay]);
  }
  else
  {
    strcpy(buffer, "   ");
  }

  itoa(iDate->Day, temp, 10);

  if (iDate->Day < 10)
  {
    strcat(buffer, " 0");
  }
  else
  {
    strcat(buffer, "  ");
  }
  strcat(buffer, temp);

  if (iDate->Month < 10)
  {
    strcat(buffer, ".0");
  }
  else
  {
    strcat(buffer, ".");
  }
  itoa(iDate->Month, temp, 10);
  strcat(buffer, temp);

  if (iDate->Year < 10)
  {
    strcat(buffer, ".0");
  }
  else
  {
    strcat(buffer, ".");
  }
  itoa(iDate->Year, temp, 10);
  strcat(buffer, temp);
}

static void DsoClearLine( uint8_t iLine_u8 )
{
  if( DISP_HEIGHT > iLine_u8 )
  {
    lcd_gotoxy( 0, iLine_u8 );
#if DISP_WIDTH == 20
    lcd_puts_P( "                    " );
#endif
  }
}

void MenuWriteMenu( char iMenu_str[], uint8_t iLength_u8 )
{
  /* clear screen */
  lcd_clrscr();
  /* place the cursor to the half of the MENU_LINE_U8 */
  lcd_gotoxy( ((DISP_WIDTH - iLength_u8) >> 1), MENU_LINE_U8 );
  /* write Menu entry */
  lcd_puts( iMenu_str );
}
